package com.heiyu.api.controller;

import com.google.common.collect.Lists;
import com.heiyu.api.request.AssessListRequest;
import com.heiyu.api.request.AssessPutRequest;
import com.heiyu.api.response.AssessListResponse;
import com.heiyu.api.response.CommonResponse;
import com.heiyu.api.response.model.AssessEntity;
import com.heiyu.api.response.model.AssessListModel;
import com.heiyu.constants.ResponseMsgEnum;
import com.heiyu.controller.ApiBaseController;
import com.heiyu.dao.ArticleAssessInfoMapper;
import com.heiyu.dao.ArticleInfoMapper;
import com.heiyu.entity.ArticleAssessInfo;
import com.heiyu.entity.ArticleInfo;
import com.heiyu.entity.UserInfo;
import com.heiyu.request.BaseRequest;
import com.heiyu.utils.DateUtils;
import com.heiyu.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(description = "评论相关")
@RestController
@RequestMapping("api/v1/assess")
public class AssessController extends ApiBaseController {

    @Autowired
    private ArticleAssessInfoMapper articleAssessInfoMapper;
    @Autowired
    private ArticleInfoMapper articleInfoMapper;

    @ApiOperation(value = "list",notes = "评论列表")
    @RequestMapping(value = "list")
    public AssessListResponse list(@ModelAttribute AssessListRequest request){
        AssessListResponse response = new AssessListResponse();
        response.setData(new AssessListModel());
        response.getData().setList(Lists.newArrayList());

        Map<String,Object> param = PageUtils.newParam(request.getPageIndex(),request.getPageSize());
        param.put("articleId",request.getArticleId());
        ArticleInfo articleInfo = this.articleInfoMapper.selectPrimary(request.getArticleId());
        if(articleInfo == null){
            response.error("文章信息不存在");
        }else{
            ArticleAssessInfo assessInfo = new ArticleAssessInfo();
            assessInfo.setArticleId(request.getArticleId());
            List<ArticleAssessInfo> list = this.articleAssessInfoMapper.listArticle(param);
            if(!CollectionUtils.isEmpty(list)){
                AssessEntity entity;
                for(ArticleAssessInfo info:list){
                    entity = new AssessEntity();
                    entity.setAssessId(info.getId());
                    entity.setContent(new String(Base64Utils.decode(info.getContent().getBytes())));
                    entity.setAvator(info.getAvator());
                    entity.setCreatedTime(DateUtils.format(info.getCreatedTime(),"yyyy.MM.dd"));
                    entity.setCreatedTimeUnix(Integer.valueOf(info.getCreatedTime().getTime()/1000+""));
                    entity.setNickName(info.getNickName());
                    response.getData().getList().add(entity);
                }

            }
            response.success();
        }
        return response;
    }

    @ApiOperation(value = "put",notes = "评论")
    @PostMapping(value = "put")
    public CommonResponse put(@RequestBody AssessPutRequest request){
        CommonResponse response = new CommonResponse();
        UserInfo userInfo = getUser();
        if(userInfo == null){
            response.error(ResponseMsgEnum.USER_NOT_LOGIN);
            return response;
        }
        if(StringUtils.isEmpty(request.getContent())){
            response.error("评论内容不能为空");
            return response;
        }
        if(request.getContent().length()>100){
            response.error("评论内容不能超过100字");
            return response;
        }

        ArticleInfo articleInfo = this.articleInfoMapper.selectPrimary(request.getArticleId());
        if(articleInfo == null){
            response.error("文章信息不存在");
        }else{
            ArticleAssessInfo assessInfo = new ArticleAssessInfo();
            assessInfo.setArticleId(request.getArticleId());
            assessInfo.setContent(new String(Base64Utils.encode(request.getContent().getBytes())));
            assessInfo.setAvator(userInfo.getAvator());
            assessInfo.setCreatedTime(new Date());
            assessInfo.setModifyTime(new Date());
            assessInfo.setIsDeleted("0");
            assessInfo.setNickName(getNickName(userInfo));
            assessInfo.setUserId(userInfo.getId());
            this.articleAssessInfoMapper.insert(assessInfo);
            response.success();
        }
        return response;
    }

}
